package graphic;

import graphic.dialogs.NewFileDialog;
import graphic.dialogs.ProgramConfigurationDialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

import utility.GraphicUtilsFactory;
import core.Client;
import core.Program;

public class ToolBar extends JToolBar {

	protected static ToolBar TOOLBAR;
	
	HashMap<String, JButton> buttons;
	
	public ToolBar()
	{
		this.buttons = new HashMap<String, JButton>();
		
		this.addClientButtons();
		super.addSeparator();
		this.addProgramButtons();
		
		super.validate();
		super.repaint();
	}
	
	protected void addClientButtons()
	{
		JButton button;
		
		/* OPEN CHAT */
		button = GraphicUtilsFactory.createSimpleToolBarButton("openChat.jpg", "Open the Chat", "Open the Chat");
		button.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if(!UsersPanel.getInstance().getSelecteUserID().isEmpty())
					ChatDialogPanel.getIstance().openChat(UsersPanel.getInstance().getSelecteUserID());	
			}
		});
		
		this.buttons.put("openChat", button);
		super.add(button);
	}
	
	protected void addProgramButtons()
	{	
		JButton button;
		
		/* OPEN A FILE */
		button = GraphicUtilsFactory.createSimpleToolBarButton("openFile.jpg", "Open File", "Open File");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Client.getInstance().requestFileLock(false);	
			}
		});
		
		this.buttons.put("openRelease", button);
		super.add(button);
		
		/* OPEN A RELEASE */
		button = GraphicUtilsFactory.createSimpleToolBarButton("openRelease.jpg", "Open Release", "Open Release");
		button.addActionListener(new ActionListener()
		{	
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Client.getInstance().requestFileLock(true);	
			}
		});
		
		this.buttons.put("openRelease", button);
		super.add(button);

		/* CREATE A FILE */
		button = GraphicUtilsFactory.createSimpleToolBarButton("newFile.jpg", "Create a File", "Create a File");
		button.addActionListener(new ActionListener()
		{	
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Program program = Client.getInstance().getAssociatedPrograms().get(FilesTabbedPanel.getIstance().getSelectedProgramName());
				
				if(program == null)
				{
					MainFrame.getInstance().showMessage("No Program has been selected!", JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!program.isNewFilesAllowed())
					MainFrame.getInstance().showMessage("New files creation not allowed!", JOptionPane.WARNING_MESSAGE);
				else
					new NewFileDialog();	
			}
		});				
		this.buttons.put("createFile", button);
		super.add(button);
	
	/* PROGRAM SETTINGS */
	button = GraphicUtilsFactory.createSimpleToolBarButton("programSettings.jpg", "Change Program Settings", "Program Settings");
	button.addActionListener(new ActionListener()
	{	
		@Override
		public void actionPerformed(ActionEvent e)
		{
			Program program = Client.getInstance().getAssociatedPrograms().get(FilesTabbedPanel.getIstance().getSelectedProgramName());
			
			if(program == null)
			{
				MainFrame.getInstance().showMessage("No Program has been selected!", JOptionPane.WARNING_MESSAGE);
				return;
			}
			
			new ProgramConfigurationDialog();
		}
	});				
	this.buttons.put("createFile", button);
	super.add(button);
}
	
	public static ToolBar getIstance()
	{
		if(TOOLBAR == null) TOOLBAR = new ToolBar();
		return TOOLBAR;
	}	
}